package com.ys.peony.car300;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.contract.BasicActivity;
import com.ys.peony.base.contract.CarPledgeContract;
import com.ys.peony.model.domain.BaoDanAddInfo;
import com.ys.peony.model.domain.CarDetailModel;
import com.ys.peony.presenter.CarPledgePresenter;
import com.ys.peony.ui.widget.DatePicker;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.ToastUtils;
import com.ys.peony.utils.YsUtil;
import com.ys.peony.view.YsDialogItemModel;
import com.ys.peony.view.inter.YsDailogClickListener;

import java.util.List;

import butterknife.BindView;

public class AddBaoDanActivity extends BasicActivity<CarPledgeContract.iAddBaoDanView, CarPledgePresenter> implements
        DatePicker.DatePickerCallBack, CarPledgeContract.iAddBaoDanView, View.OnClickListener, YsDailogClickListener {

    @BindView(R.id.rootview)
    ScrollView rootView;
    @BindView(R.id.tax_company)
    RelativeLayout tax_company;
    @BindView(R.id.tax_start)
    RelativeLayout tax_start;
    @BindView(R.id.tax_end)
    RelativeLayout tax_end;
    @BindView(R.id.tax_time)
    RelativeLayout tax_time;
    @BindView(R.id.tax_type)
    RelativeLayout tax_type;


    @BindView(R.id.tax_price_all_et)
    EditText tax_price_all_et;
    @BindView(R.id.number_et)
    EditText number_et;
    @BindView(R.id.tax_yes_num_et)
    EditText tax_yes_num_et;
    @BindView(R.id.tax_number_et)
    EditText tax_number_et;
    @BindView(R.id.toubaoren_et)
    EditText toubaoren_et;
    @BindView(R.id.beitoubaoren_et)
    EditText beitoubaoren_et;
    @BindView(R.id.beibao_idcard_et)
    EditText beibao_idcard_et;
    @BindView(R.id.address_et)
    EditText address_et;
    @BindView(R.id.phone_et)
    EditText phone_et;
    @BindView(R.id.fudng_percent_et)
    EditText fudng_percent_et;
    @BindView(R.id.car_tax_dnyj_et)
    EditText car_tax_dnyj_et;
    @BindView(R.id.car_tax_wnbj_et)
    EditText car_tax_wnbj_et;
    @BindView(R.id.car_tax_znj_et)
    EditText car_tax_znj_et;
    @BindView(R.id.car_tax_all_et)
    EditText car_tax_all_et;
    @BindView(R.id.car_tax_wspz_et)
    EditText car_tax_wspz_et;
    @BindView(R.id.pzkjjg_et)
    EditText pzkjjg_et;

    @BindView(R.id.tax_company_tv)
    TextView tax_company_tv;
    @BindView(R.id.tax_start_tv)
    TextView tax_start_tv;
    @BindView(R.id.tax_end_tv)
    TextView tax_end_tv;
    @BindView(R.id.tax_time_tv)
    TextView tax_time_tv;
    @BindView(R.id.tax_type_tv)
    TextView tax_type_tv;

    @BindView(R.id.save)
    TextView save;


    public final static String PARAM_VEHICLE_ID = "vehicle_id";
    public final static String PARAM_BAODAN_INDEX = "baodan_index";
    public final static String PARAM_CAR_INDEX = "car_index";
    private String mVehicle_id = ""; //车辆id
    private String mGuaranteeID = ""; //保单id
    private String mBaodanIndex = "";
    private String mCarIndex = "";

    private DatePicker datePicker = null;
    private YsDialogItemModel mDialogItemModel;
    private String selectYearStr = "", selectMonthStr = "", selectDayStr = "";

    @Override
    protected CarPledgePresenter initPresenter() {
        return new CarPledgePresenter();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_add_bao_dan;
    }


    @Override
    protected void initView() {
        super.initView();
        initData();
        initWidget();
        initListener();

    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            mVehicle_id = bundle.getString(PARAM_VEHICLE_ID, "");
            mBaodanIndex = bundle.getString(PARAM_BAODAN_INDEX, "");
            mCarIndex = bundle.getString(PARAM_CAR_INDEX, "");
            if (!TextUtils.isEmpty(mVehicle_id) && !TextUtils.isEmpty(mBaodanIndex) && !TextUtils.isEmpty(mCarIndex)) {
                insertData();
            }
        }
    }

    private void insertData() {
        CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean vehicleSlipsBean = null;
        CarDetailModel carDetailModel = CarDataManager.getInstance().getCarDetailModel();
        CarDetailModel.BusiMortgageVoBean busiMortgageVoBean = carDetailModel.getBusiMortgageVo();
        if (busiMortgageVoBean != null) {
            List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> lists = busiMortgageVoBean.getBusivehicleandslipvoList();
            if (lists != null && lists.size() > 0) {
                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBeen = lists.get(Integer.valueOf(mCarIndex)).get(0);
                vehicleSlipsBean = busivehicleandslipvoListBeen.getVehicleSlips().get(Integer.valueOf(mBaodanIndex));
            }
        }
        if (vehicleSlipsBean == null) {
            return;
        }
        mGuaranteeID = String.valueOf(vehicleSlipsBean.getId());
        tax_price_all_et.setText(String.valueOf(vehicleSlipsBean.getGuarantee_fee()));
        number_et.setText(vehicleSlipsBean.getSerial_number().toString().trim());
        tax_yes_num_et.setText(vehicleSlipsBean.getGuarantee_slip_code().toString().trim());
        tax_number_et.setText(vehicleSlipsBean.getGuarantee_slip_number().toString().trim());
        toubaoren_et.setText(vehicleSlipsBean.getPolicy_holder().toString().trim());
        beitoubaoren_et.setText(vehicleSlipsBean.getInsurant().toString().trim());
        beibao_idcard_et.setText(vehicleSlipsBean.getInsurant_id_card().toString().trim());
        address_et.setText(vehicleSlipsBean.getAddress().toString().trim());
        phone_et.setText(vehicleSlipsBean.getPhone().toString().trim());
        fudng_percent_et.setText(vehicleSlipsBean.getFloating_ratio().toString().trim());
        car_tax_dnyj_et.setText(String.valueOf(vehicleSlipsBean.getTax_this_year()));
        car_tax_wnbj_et.setText(String.valueOf(vehicleSlipsBean.getTax_previous_year()));
        car_tax_znj_et.setText(String.valueOf(vehicleSlipsBean.getTax_late_fee()));
        car_tax_all_et.setText(String.valueOf(vehicleSlipsBean.getTax_total()));
        car_tax_wspz_et.setText(vehicleSlipsBean.getTax_proof().toString().trim());
        pzkjjg_et.setText(vehicleSlipsBean.getProof_office().toString().trim());
        tax_company_tv.setText(vehicleSlipsBean.getInsurance_company_desr().toString().trim());
        tax_start_tv.setText(YsUtil.getDateToString(vehicleSlipsBean.getGuarantee_begin().getTime()).toString().trim());
        tax_end_tv.setText(YsUtil.getDateToString(vehicleSlipsBean.getGuarantee_end().getTime()).toString().trim());
        tax_time_tv.setText(YsUtil.getDateToString(vehicleSlipsBean.getSigning_time().getTime()).toString().trim());
        String tax_type_id = vehicleSlipsBean.getGuarantee_slip_type();
        String tax_type_text = presenter.getItemTextById(carDetailModel.getGuaranteeSlipGuaranteeSlipTypeList(), R.id.tax_type_tv, tax_type_id);
        tax_type_tv.setText(tax_type_text);

        //保存数据
        CarDataManager.getInstance().getmBaoDanAddInfo().setInsurance_company(vehicleSlipsBean.getInsurance_company());
        CarDataManager.getInstance().getmBaoDanAddInfo().setGuarantee_slip_type(vehicleSlipsBean.getGuarantee_slip_type());
    }

    private void initWidget() {
        mDialogItemModel = new YsDialogItemModel(this, this);
    }

    private void initListener() {
        tax_company.setOnClickListener(this);
        tax_start.setOnClickListener(this);
        tax_end.setOnClickListener(this);
        tax_time.setOnClickListener(this);
        tax_type.setOnClickListener(this);
        tax_price_all_et.setOnClickListener(this);
        save.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        datePicker = null;
        List<ItemModel> itemModels = null;
        switch (view.getId()) {
            case R.id.tax_company:
                itemModels = presenter.getItemModel(CarDataManager.getInstance().getCarDetailModel().getGuaranteeSlipInsuranceCompanyList(), R.id.tax_company);
                if (itemModels != null) {
                    mDialogItemModel.setItemModels(itemModels, YSConstant.POP_TYPE_TAXCOMPANY);
                }
                break;
            case R.id.tax_start:
                if (datePicker == null) {
                    datePicker = new DatePicker(this, this, YSConstant.POP_DATE_TAX_START);
                }
                datePicker.show(rootView, selectYearStr, selectMonthStr, selectDayStr);
                break;
            case R.id.tax_end:
                if (datePicker == null) {
                    datePicker = new DatePicker(this, this, YSConstant.POP_DATE_TAX_END);
                }
                datePicker.show(rootView, selectYearStr, selectMonthStr, selectDayStr);
                break;
            case R.id.tax_time:
                if (datePicker == null) {
                    datePicker = new DatePicker(this, this, YSConstant.POP_DATE_TAX_TIME);
                }
                datePicker.show(rootView, selectYearStr, selectMonthStr, selectDayStr);
                break;
            case R.id.tax_type:
                itemModels = presenter.getItemModel(CarDataManager.getInstance().getCarDetailModel().getGuaranteeSlipGuaranteeSlipTypeList(), R.id.tax_type);
                if (itemModels != null) {
                    mDialogItemModel.setItemModels(itemModels, YSConstant.POP_TYPE_TAXTYPE);
                }
                break;

            case R.id.tax_price_all_et:
                tax_price_all_et.setCursorVisible(true);
                break;
            case R.id.save:
                if (checkBaoDanInfo()) {
                    LoadingProgressUtils.showProgressDialog(this);
                    saveBaoDanInfo();
                    if (!TextUtils.isEmpty(mVehicle_id)) {
                        presenter.addBaoDanInfo(mVehicle_id,mGuaranteeID);
                    }

                } else {
                    ToastUtils.showToast("请完善必要信息");
                }
                break;
        }
    }


    private boolean checkBaoDanInfo() {
        if (TextUtils.isEmpty(tax_company_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(tax_start_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(tax_end_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(tax_time_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(tax_type_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(tax_price_all_et.getText().toString().trim())) {
            return false;
        }
        return true;
    }

    private void saveBaoDanInfo() {
        BaoDanAddInfo baoDanAddInfo = CarDataManager.getInstance().getmBaoDanAddInfo();
        baoDanAddInfo.setInsurance_company_desr(tax_company_tv.getText().toString().trim());
        baoDanAddInfo.setGuarantee_fee(tax_price_all_et.getText().toString().trim());
        baoDanAddInfo.setGuarantee_begin(tax_start_tv.getText().toString().trim());
        baoDanAddInfo.setGuarantee_end(tax_end_tv.getText().toString().trim());
        baoDanAddInfo.setSigning_time(tax_time_tv.getText().toString().trim());

        baoDanAddInfo.setSerial_number(number_et.getText().toString().trim());
        baoDanAddInfo.setGuarantee_slip_code(tax_yes_num_et.getText().toString().trim());
        baoDanAddInfo.setGuarantee_slip_number(tax_number_et.getText().toString().trim());
        baoDanAddInfo.setPolicy_holder(toubaoren_et.getText().toString().trim());
        baoDanAddInfo.setInsurant(beitoubaoren_et.getText().toString().trim());
        baoDanAddInfo.setInsurant_id_card(beibao_idcard_et.getText().toString().trim());
        baoDanAddInfo.setAddress(address_et.getText().toString().trim());
        baoDanAddInfo.setPhone(phone_et.getText().toString().trim());
        baoDanAddInfo.setFloating_ratio(fudng_percent_et.getText().toString().trim());
        baoDanAddInfo.setTax_this_year(car_tax_dnyj_et.getText().toString().trim());
        baoDanAddInfo.setTax_previous_year(car_tax_wnbj_et.getText().toString().trim());
        baoDanAddInfo.setTax_late_fee(car_tax_znj_et.getText().toString().trim());
        baoDanAddInfo.setTax_total(car_tax_all_et.getText().toString().trim());
        baoDanAddInfo.setTax_proof(car_tax_wspz_et.getText().toString().trim());
        baoDanAddInfo.setProof_office(pzkjjg_et.getText().toString().trim());
    }

    @Override
    public void datePickerConfirmOnclick(String yearStr, String monthStr, String dayStr, int tag) {
        selectYearStr = yearStr;
        selectMonthStr = monthStr;
        selectDayStr = dayStr;
        String time = yearStr + "-" + monthStr + "-" + dayStr;
        switch (tag) {
            case YSConstant.POP_DATE_TAX_START:
                tax_start_tv.setText(time);
                break;
            case YSConstant.POP_DATE_TAX_END:
                tax_end_tv.setText(time);
                break;
            case YSConstant.POP_DATE_TAX_TIME:
                tax_time_tv.setText(time);
                break;

        }
    }

    @Override
    public void datePickerCancelOnclick() {

    }


    @Override
    public void clickDialogItemModel(int type, ItemModel itemModel) {
        if (itemModel == null || TextUtils.isEmpty(itemModel.getVal()) || TextUtils.isEmpty(itemModel.getText())) {
            return;
        }
        switch (type) {
            case YSConstant.POP_TYPE_TAXCOMPANY:
                tax_company_tv.setText(itemModel.getText());
                CarDataManager.getInstance().getmBaoDanAddInfo().setInsurance_company(itemModel.getVal());
                break;
            case YSConstant.POP_TYPE_TAXTYPE:
                tax_type_tv.setText(itemModel.getText());
                CarDataManager.getInstance().getmBaoDanAddInfo().setGuarantee_slip_type(itemModel.getVal());
                break;
        }
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showToast(msg);
    }


    @Override
    public void onSuccess(int code) {
        switch (code) {
            case CarPledgeContract.ADD_CAR_BAODAN:
                presenter.getCarInfoById(CarDataManager.getInstance().getLoanId());
                break;
            case CarPledgeContract.GET_CAR_DETAIL:
                LoadingProgressUtils.closeDialog();
                ToastUtils.showToast("保存成功");
                finish();
                break;
        }
    }
}
